<?php



namespace App\Transformer;


use App\Models\User;
use League\Fractal\TransformerAbstract;

class UserTransformer extends TransformerAbstract
{

    protected $defaultIncludes = ['department'];

    public function transform(User $data)
    {
        return [
            'id' => $data->id,
            'department_id' => $data->department_id,
            'username' => $data->username,
            'realname' => $data->realname,
            'status' => $data->status,
            'roles' => $data->roles,
            'last_login_at' => $data->last_login_at ? $data->last_login_at->toDateTimeString() : '',
            'created_at' => $data->created_at->toDateTimeString(),
        ];
    }

    public function includeDepartment(User $data)
    {
        if ($data->relationLoaded('department') && $data->department) {
            return $this->item($data->department, new DepartmentTransformer());
        }
    }

}
